class_name InputHandler
extends Node

##
var input_action_checkers: Dictionary = {}


func add_input_action(action_name: String) -> void:
	var input_action_checker: InputActionChecker = InputActionChecker.new()
	input_action_checker.action_name = action_name
	input_action_checkers[action_name] = input_action_checker


func remove_input_action(action_name: String) -> void:
	input_action_checkers.erase(action_name)


func get_action_checker(action_name: String) -> InputActionChecker:
	if input_action_checkers.has(action_name):
		return input_action_checkers[action_name]
	print("Warning: Action '%s' not found in input_action_checkers." % action_name)
	return null


func on_update(delta: float) -> void:
	for input_action_checker in input_action_checkers.values():
		input_action_checker.on_update(delta)


func is_action_pressed(action_name: String) -> bool:
	var checker = get_action_checker(action_name)
	return checker.is_pressed if checker else false


func is_action_pressed_any(action_names: Array[String]) -> bool:
	for action_name in action_names:
		if is_action_pressed(action_name):
			return true
	return false


func is_action_double_click_started(action_name: String) -> bool:
	var checker = get_action_checker(action_name)
	return checker.is_double_click_started if checker else false


func is_action_double_click_started_any(action_names: Array[String]) -> bool:
	for action_name in action_names:
		if is_action_double_click_started(action_name):
			return true
	return false


func is_action_long_pressed(action_name: String) -> bool:
	var checker = get_action_checker(action_name)
	return checker.is_long_pressed if checker else false


func is_action_released(action_name: String) -> bool:
	var checker = get_action_checker(action_name)
	return checker.is_released if checker else false


func get_axis(negative_action_name: String, positive_action_name: String) -> float:
	var axis := 0.0
	if is_action_pressed(negative_action_name):
		axis -= 1
	if is_action_pressed(positive_action_name):
		axis += 1
	return axis


func get_vector(x_negative_action_name: String, x_positive_action_name: String, y_negative_action_name: String, y_positive_action_name: String) -> Vector2:
	return Vector2(get_axis(x_negative_action_name, x_positive_action_name), get_axis(y_negative_action_name, y_positive_action_name))
